package com.archgeek.bigdata.metrics;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.exporter.MetricsServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import java.util.stream.Collector;

/**
 * @author pizhihui
 * @date 2021-12-10 17:26
 */
public class MetricMain {

    private static final int PORT = 10019;
    private static final String METRIC_PATH = "/metrics";



    public static void main(String[] args) throws Exception {


        Server server = new Server(PORT);

        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");

        server.setHandler(context);

        CollectorRegistry registry = new CollectorRegistry();


        MetricRegistry metricRegistry = new MetricRegistry();


        DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry);
        dropwizardExports.register(registry);


        MetricsServlet metricsServlet = new MetricsServlet(registry);

        context.addServlet(new ServletHolder(metricsServlet), METRIC_PATH);


        // 启动
        server.start();

        // 增加
        Counter counter = metricRegistry.counter("namespace");
        counter.inc();


    }

}
